package no18;

/**
 * @author 兴趣使然黄小黄
 * @version 1.0
 * @date 2023/5/16 22:46
 * II 有效的回文
 * https://leetcode.cn/problems/XltzEq/
 */
public class Solution2 {

    public boolean isPalindrome(String s) {
        int start = 0;
        int end = s.length() - 1;
        while (start < end) {
            // 双指针均指向数字或者字符
            while (start < end && !isAlphaOrDigit(s.charAt(start))) {
                ++start;
            }
            while (start < end && !isAlphaOrDigit(s.charAt(end))) {
                --end;
            }
            // 判断是否相同
            if (!(s.charAt(start) == s.charAt(end) || (char) (s.charAt(start)^32) == s.charAt(end))) {
                return false;
            }
            ++start;
            --end;
        }
        return true;
    }

    // 判断是否为字母或者数字
    private boolean isAlphaOrDigit(char c) {
        return 'A' <= c && c <= 'Z'
                || 'a' <= c && c <= 'z'
                || '0' <= c && c <= '9';
    }
}
